setwd("")
library(manifestoR)
library(tidyverse)
library(readstata13)
library(miceadds)
library(scales)
options(scipen=999)

# function for changing parts of a variable in tidyverse (from https://stackoverflow.com/questions/34096162/dplyr-mutate-replace-several-columns-on-a-subset-of-rows)
mutate_when <- function(data, ...) {
  dots <- eval(substitute(alist(...)))
  for (i in seq(1, length(dots), by = 2)) {
    condition <- eval(dots[[i]], envir = data)
    mutations <- eval(dots[[i + 1]], envir = data[condition, , drop = FALSE])
    data[condition, names(mutations)] <- mutations
  }
  data
}

###############################################

#### 1. DATASET CREATION #####

# Get the dataset: replication requires going to the manifesto project website, signing the user agreement and downloading the API key as per their instructions
# This uses the manifestoR package to access the CMP data
# See https://manifesto-project.wzb.eu/signup to sign up for access to CMP data, and to get an API key
# See https://manifesto-project.wzb.eu/information/documents/manifestoR for info on how to use the manifestoR package
# note that new versions of manifesto data are released and past data can be altered. Use the "2018-b" version for an exact replication of our results
# for the code below to work, the API key needs to be saved in your working directory
mp_setapikey("manifesto_apikey.txt")
mpds <- mp_maindataset(version="2018b")


# Create dependent variables (logit-scaled, negative = left); coding scheme is at https://manifesto-project.wzb.eu/coding_schemes/mp_v5

  mpds$govservices <- scale_logit(mpds,neg=c("per403","per404","per406","per412","per413","per504","per506","per701"),  #benoit + laver 2007
                                  pos=c("per401","per402","per407","per414","per504","per505")) 
  
  mpds$freemarket <- scale_logit(mpds,neg=c("per403","per412","per413","per415"),  # lowe et al 11
                                 pos=c("per401","per402"))  

  mpds$libcon <- scale_logit(mpds,neg=c("per103","per105","per106","per107","per202"),
                             pos=c("per104","per201","per203","per305","per601","per603","per605","per606"))
  
  mpds$libcon_auth <- scale_logit(mpds,neg=c("per501","per602","per604","per502","per607","per416","per705","per706","per201","per202"),
                                                           pos=c("per305","per601","per603","per605","per608","per606"))
  
  mpds$nationalism <- scale_logit(mpds,neg=c("per607","per602"),pos=c("per608","per601"))
  


# Create dataset for analysis
  manifdata <- mpds %>% filter(countryname %in% c("Austria","Belgium","Bulgaria","Cyprus","Czech Republic",
                                                  "Denmark","Estonia","Finland","France","Germany","Greece",
                                                  "Hungary","Ireland","Italy","Latvia","Lithuania","Netherlands",
                                                  "Northern Ireland","Norway","Poland","Portugal","Slovakia",
                                                  "Slovenia","Spain","Sweden","Switzerland","United Kingdom","Luxembourg") & date>197112)
  
# Sort out Germany and Northern Ireland
  manifdata <- manifdata[!(manifdata$countryname=="Germany" & manifdata$date<198912),]  # opinion data pre-90 is for all of germany
  manifdata$countryname[manifdata$countryname=="United Kingdom" & (manifdata$partyname=="We Ourselves"|manifdata$partyname=="Ulster Unionist Party"|
                                                                     manifdata$partyname=="Democratic Unionist Party"|
                                                                     manifdata$partyname=="Social Democratic and Labour Party")] <- "Northern Ireland"
  manifdata$countryname[manifdata$countryname=="United Kingdom"] <- "Great Britain"
  
  # Combine UK Liberal and SDP parties' vote shares pre 92: stood on identical manifestos
  manifdata$pervote[which(manifdata$party==51420 & manifdata$date==198306)] <- 11.642+13.727
  manifdata$pervote[which(manifdata$party==51420 & manifdata$date==198706)] <- 9.739+12.830
  manifdata$party[manifdata$party==51420] <- 51421
  manifdata <- manifdata[-which((manifdata$party==51330 & manifdata$date==198306)),]
  manifdata <- manifdata[-which((manifdata$party==51330 & manifdata$date==198706)),]
  
# France: RPR name change
  manifdata$partyabbrev[manifdata$party==31621 & manifdata$date==198106] <- "RPR"
  manifdata$partyabbrev[manifdata$party==31621 & manifdata$date==198603] <- "RPR"
  
  manifdata$party[manifdata$partyname=="Union for a New Majority - Gaullists/Conservatives"] <- 31625

# Year
  manifdata$year <-  rep(NA,length(manifdata$countryname))
  for(i in 1:length(manifdata$date)){
       manifdata$year[i] =paste(unlist(strsplit(as.character(manifdata$date[i]),split = character(0)))[1:4],sep="", collapse="")
    }
    
# Opinion data; merge into manifesto data
  load("europe_ideology.RData")
  
  for(i in 1:length(e$biennium)){
    e$year[i] =paste(unlist(strsplit(as.character(e$biennium[i]),split = character(0)))[6:9],sep="", collapse="")
  }
  
  e$year <- as.numeric(e$year)
  manifdata$year <- as.numeric(manifdata$year)
  manifdata$year.alt <- manifdata$year-1
  
  manifdata <- subset(manifdata, select=c("countryname","partyname","year","year.alt","edate","party","partyabbrev","parfam","pervote",
                                          "govservices","freemarket","libcon","libcon_auth","nationalism","rile"))
  
  manifdata$position.econabs=manifdata$position.econrel=manifdata$position.soc=manifdata$position.imm <- rep(NA,length(manifdata$year))
  
  for(i in 1:length(manifdata$year)){
    
    if(manifdata$year[i] %in% unique(e$year) & manifdata$countryname[i]!="Luxembourg"){
      manifdata$position.econabs[i] <- e$econ_abs_post_mean[which(e$country==manifdata$countryname[i] & 
                                                                    e$year==manifdata$year[i])]
      manifdata$position.econrel[i] <- e$econ_rel_post_mean[which(e$country==manifdata$countryname[i] & 
                                                                    e$year==manifdata$year[i])]
      manifdata$position.soc[i] <- e$soc_post_mean[which(e$country==manifdata$countryname[i] & 
                                                           e$year==manifdata$year[i])]
      manifdata$position.imm[i] <- e$immi_post_mean[which(e$country==manifdata$countryname[i] & 
                                                            e$year==manifdata$year[i])]
    }
    
    else if (manifdata$year.alt[i] %in% unique(e$year) & manifdata$countryname[i]!="Luxembourg"){
      manifdata$position.econabs[i] <- e$econ_abs_post_mean[which(e$country==manifdata$countryname[i] & 
                                                                    e$year==manifdata$year.alt[i])]
      manifdata$position.econrel[i] <- e$econ_rel_post_mean[which(e$country==manifdata$countryname[i] & 
                                                                    e$year==manifdata$year.alt[i])]
      manifdata$position.soc[i] <- e$soc_post_mean[which(e$country==manifdata$countryname[i] & 
                                                           e$year==manifdata$year.alt[i])]
      manifdata$position.imm[i] <- e$immi_post_mean[which(e$country==manifdata$countryname[i] & 
                                                            e$year==manifdata$year.alt[i])]
    }
  }
  

  
# Remove parties with only one election
  nelections <- manifdata %>% group_by(party) %>% summarise(nelec = length(pervote))
  manifdata$nelec <- rep(NA,length(manifdata$countryname))
  for(i in 1:length(manifdata$countryname)){
    manifdata$nelec[i] <- nelections$nelec[nelections$party==manifdata$party[i]]
  }
  
  manifdata <- manifdata %>% filter(nelec>1)
  
# calc average percentage of the vote
  percvote <- manifdata %>% group_by(party) %>% summarise(percvote = mean(pervote,na.rm=T))
  
  percvote$country=percvote$partyname <- c()
  for(i in 1:length(percvote$party)){
    percvote$partyname[i] <- manifdata$partyname[manifdata$party==percvote$party[i]]
    percvote$country[i] <- manifdata$countryname[manifdata$party==percvote$party[i]]
  }
  
  manifdata$vote_av <- rep(NA,length(manifdata$countryname))
  for(i in 1:length(manifdata$countryname)){
    manifdata$vote_av[i] <- percvote$percvote[percvote$party==manifdata$party[i]]
  }
  
  
  
# Take first differences and lags
  manifdata <- manifdata[with(manifdata, order(manifdata$party)), ]
  
  fd_vote=fd_vote_lag=fd_position.imm=fd_position.soc=fd_position.soc_auth=fd_position.econrel=fd_position.econabs <- c()
  fd_govservices=fd_freemarket=fd_libcon=fd_libcon_auth=fd_nationalism <- c()
  fd_govservices_lag=fd_freemarket_lag=fd_libcon_lag=fd_libcon_auth_lag=fd_nationalism_lag <- c()
  
  for(i in 1:length(unique(manifdata$party))){
    govservices <- c(NA, diff(manifdata$govservices[which(manifdata$party==unique(manifdata$party)[i])]) )
    govservices_lag <- c(NA,govservices[1:(length(govservices)-1)])
    freemarket <- c(NA, diff(manifdata$freemarket[which(manifdata$party==unique(manifdata$party)[i])]) )
    freemarket_lag <- c(NA,freemarket[1:(length(freemarket)-1)])
    libcon <- c(NA, diff(manifdata$libcon[which(manifdata$party==unique(manifdata$party)[i])]) )
    libcon_lag <- c(NA,libcon[1:(length(libcon)-1)])
    libcon_auth <- c(NA, diff(manifdata$libcon_auth[which(manifdata$party==unique(manifdata$party)[i])]) )
    libcon_auth_lag <- c(NA,libcon_auth[1:(length(libcon_auth)-1)])
    nationalism <- c(NA, diff(manifdata$nationalism[which(manifdata$party==unique(manifdata$party)[i])]) )
    nationalism_lag <- c(NA,nationalism[1:(length(nationalism)-1)])
    
    vote <- c(NA, diff(manifdata$pervote[which(manifdata$party==unique(manifdata$party)[i])]) )
    vote_lag <- c(NA,vote[1:(length(vote)-1)])
    
    imm <- c(NA, diff(manifdata$position.imm[which(manifdata$party==unique(manifdata$party)[i])]) )
    soc <- c(NA, diff(manifdata$position.soc[which(manifdata$party==unique(manifdata$party)[i])]) )
    econrel <- c(NA, diff(manifdata$position.econrel[which(manifdata$party==unique(manifdata$party)[i])]) )
    econabs <- c(NA, diff(manifdata$position.econabs[which(manifdata$party==unique(manifdata$party)[i])]) )
    
    fd_govservices <- c(fd_govservices,govservices)
    fd_govservices_lag <- c(fd_govservices_lag,govservices_lag)
    fd_freemarket <- c(fd_freemarket,freemarket)
    fd_freemarket_lag <- c(fd_freemarket_lag,freemarket_lag)
    fd_libcon <- c(fd_libcon,libcon)
    fd_libcon_lag <- c(fd_libcon_lag,libcon_lag)
    fd_libcon_auth <- c(fd_libcon_auth,libcon_auth)
    fd_libcon_auth_lag <- c(fd_libcon_auth_lag,libcon_auth_lag)
    fd_nationalism <- c(fd_nationalism,nationalism)
    fd_nationalism_lag <- c(fd_nationalism_lag,nationalism_lag)
    
    fd_vote <- c(fd_vote,vote)
    fd_vote_lag <- c(fd_vote_lag,vote_lag)
    
    fd_position.imm <- c(fd_position.imm,imm)
    fd_position.soc <- c(fd_position.soc,soc)
    fd_position.econrel <- c(fd_position.econrel,econrel)
    fd_position.econabs <- c(fd_position.econabs,econabs)
  }
  
  # in dataset
  manifdata$pervote_fd <- fd_vote
  manifdata$pervote_fd_lag <- fd_vote_lag
  
  manifdata$govservices_fd <- fd_govservices
  manifdata$freemarket_fd <- fd_freemarket
  manifdata$libcon_fd <- fd_libcon
  manifdata$libcon_auth_fd <- fd_libcon_auth
  manifdata$nationalism_fd <- fd_nationalism

  manifdata$govservices_fd_lag <- fd_govservices_lag
  manifdata$freemarket_fd_lag <- fd_freemarket_lag
  manifdata$libcon_fd_lag <- fd_libcon_lag
  manifdata$libcon_auth_fd_lag <- fd_libcon_auth_lag
  manifdata$nationalism_fd_lag <- fd_nationalism_lag
  
  manifdata$position.imm_fd <- fd_position.imm
  manifdata$position.soc_fd <- fd_position.soc
  manifdata$position.econrel_fd <- fd_position.econrel
  manifdata$position.econabs_fd <- fd_position.econabs
  
  rm(fd_vote,fd_vote_lag,vote,vote_lag,imm,soc,econrel,econabs,fd_position.imm,fd_position.soc,
     fd_position.econrel,fd_position.econabs,fd_govservices,fd_freemarket,fd_libcon,fd_libcon_auth,fd_nationalism,fd_govservices_lag,fd_freemarket_lag,
     fd_libcon_lag,fd_libcon_auth_lag,fd_nationalism_lag,freemarket,freemarket_lag,govservices,govservices_lag,libcon,libcon_lag,libcon_auth,
     libcon_auth_lag,nationalism,nationalism_lag)
  
# niche party dummies,interactions
  manifdata$niche <- ifelse((manifdata$parfam==10|manifdata$parfam==20|manifdata$parfam==70),1,0)
  
  manifdata$niche_position.immfd <- manifdata$niche*manifdata$position.imm_fd
  manifdata$niche_position.socfd <- manifdata$niche*manifdata$position.soc_fd
  manifdata$niche_position.econrelfd <- manifdata$niche*manifdata$position.econrel_fd
  manifdata$niche_position.econabsfd <- manifdata$niche*manifdata$position.econabs_fd
  
  manifdata$pervote_govservices_lag <- manifdata$govservices_fd_lag*manifdata$pervote_fd_lag
  manifdata$pervote_freemarket_lag <- manifdata$freemarket_fd_lag*manifdata$pervote_fd_lag
  manifdata$pervote_libcon_lag <- manifdata$libcon_fd_lag*manifdata$pervote_fd_lag
  manifdata$pervote_libcon_auth_lag <- manifdata$libcon_auth_fd_lag*manifdata$pervote_fd_lag
  manifdata$pervote_nationalism_lag <- manifdata$nationalism_fd_lag*manifdata$pervote_fd_lag
  
  
# Create clusters; cut down years  
  manifdata$clusters <- c(rep(NA, length(manifdata$countryname)))
  max_index <- 0
  
  #loop through countries, then years
  for(i in 1:length(unique(manifdata$countryname))){
    
    years <- c(sort(unique(manifdata$year[manifdata$countryname==unique(manifdata$countryname)[i]])))
    
    # assign a cluster number to each observation for country i
    for(j in 1:length(manifdata$countryname[manifdata$countryname==unique(manifdata$countryname)[i]])){
      manifdata$clusters[manifdata$countryname==unique(manifdata$countryname)[i]][j] <- max_index + 
        which(years == manifdata$year[manifdata$countryname==unique(manifdata$countryname)[i]][j])
    }
    # re-set max index each time so that we get unique clusters for each country
    max_index <- max_index + length(unique(manifdata$year[manifdata$countryname==unique(manifdata$countryname)[i]]))
  }
  
# 'benign' and 'harmful' pub opn shifts
  
  # Left and right dummies
  manifdata$leftparty <- ifelse((manifdata$parfam==10|manifdata$parfam==20|manifdata$parfam==30),1,0)
  manifdata$rightparty <- ifelse((manifdata$parfam==50|manifdata$parfam==60|manifdata$parfam==70),1,0)
  
  #'disadvantaged' dummy, absolute econ
  manifdata$disad_econabs <- rep(NA, length(manifdata$countryname))
  for(i in 1:length(manifdata$countryname)){
    
    if(is.na(manifdata$position.econabs_fd[i])==FALSE & manifdata$leftparty[i] == 1 & manifdata$position.econabs_fd[i]>0){
      manifdata$disad_econabs[i] <- 1
    } 
    
    else if(is.na(manifdata$position.econabs_fd[i])==FALSE & manifdata$rightparty[i] == 1 & manifdata$position.econabs_fd[i]<0) {
      manifdata$disad_econabs[i] <- 1
    }
    
    else{manifdata$disad_econabs[i] <- 0}
    
  }
  
  # 1 minus it
  manifdata$not_disad_econabs <- 1 - manifdata$disad_econabs
  
  # interactions with pub opn shifts
  manifdata$harmful_econabs <- manifdata$disad_econabs*manifdata$position.econabs_fd
  manifdata$benign_econabs <- manifdata$not_disad_econabs*manifdata$position.econabs_fd
  
  #####
  
  #'disadvantaged' dummy, relative econ
  manifdata$disad_econrel <- rep(NA, length(manifdata$countryname))
  for(i in 1:length(manifdata$countryname)){
    
    if(is.na(manifdata$position.econrel_fd[i])==FALSE & manifdata$leftparty[i] == 1 & manifdata$position.econrel_fd[i]>0){
      manifdata$disad_econrel[i] <- 1
    } 
    
    else if(is.na(manifdata$position.econrel_fd[i])==FALSE & manifdata$rightparty[i] == 1 & manifdata$position.econrel_fd[i]<0) {
      manifdata$disad_econrel[i] <- 1
    }
    
    else{manifdata$disad_econrel[i] <- 0}
    
  }
  
  # 1 minus it
  manifdata$not_disad_econrel <- 1 - manifdata$disad_econrel
  
  # interactions with pub opn shifts
  manifdata$harmful_econrel <- manifdata$disad_econrel*manifdata$position.econrel_fd
  manifdata$benign_econrel <- manifdata$not_disad_econrel*manifdata$position.econrel_fd
  
  #####
  
  #'disadvantaged' dummy, social
  manifdata$disad_soc <- rep(NA, length(manifdata$countryname))
  for(i in 1:length(manifdata$countryname)){
    
    if(is.na(manifdata$position.soc_fd[i])==FALSE & manifdata$leftparty[i] == 1 & manifdata$position.soc_fd[i]>0){
      manifdata$disad_soc[i] <- 1
    } 
    
    else if(is.na(manifdata$position.soc_fd[i])==FALSE & manifdata$rightparty[i] == 1 & manifdata$position.soc_fd[i]<0) {
      manifdata$disad_soc[i] <- 1
    }
    
    else{manifdata$disad_soc[i] <- 0}
    
  }
  
  # 1 minus it
  manifdata$not_disad_soc <- 1 - manifdata$disad_soc
  
  # interactions with pub opn shifts
  manifdata$harmful_soc <- manifdata$disad_soc*manifdata$position.soc_fd
  manifdata$benign_soc <- manifdata$not_disad_soc*manifdata$position.soc_fd
  
  #####
  
  #'disadvantaged' dummy, immigration
  manifdata$disad_imm <- rep(NA, length(manifdata$countryname))
  for(i in 1:length(manifdata$countryname)){
    
    if(is.na(manifdata$position.imm_fd[i])==FALSE & manifdata$leftparty[i] == 1 & manifdata$position.imm_fd[i]>0){
      manifdata$disad_imm[i] <- 1
    } 
    
    else if(is.na(manifdata$position.imm_fd[i])==FALSE & manifdata$rightparty[i] == 1 & manifdata$position.imm_fd[i]<0) {
      manifdata$disad_imm[i] <- 1
    }
    
    else{manifdata$disad_imm[i] <- 0}
    
  }
  
  # 1 minus it
  manifdata$not_disad_imm <- 1 - manifdata$disad_imm
  
  # interactions with pub opn shifts
  manifdata$harmful_imm <- manifdata$disad_imm*manifdata$position.imm_fd
  manifdata$benign_imm <- manifdata$not_disad_imm*manifdata$position.imm_fd
  

 
###############################################
  
  #### 2. ANALYSIS ##### 
 
 
  # domain-specific representation with the new scales: general
  reg8 <- lm.cluster(freemarket_fd ~ 0 + position.econabs_fd + freemarket_fd_lag + pervote_fd_lag + 
                       pervote_freemarket_lag + factor(countryname),
                     data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                    party==13951, list(parfam=70),
                                                    party==14820, list(niche=1),
                                                    party==14820, list(parfam=70)) %>% 
                       filter(vote_av>=1 & parfam<80),
                     cluster="clusters")
  summary(reg8)
  length(reg8$lm_res[[2]])  
  
  reg9 <- lm.cluster(govservices_fd ~ 0 + position.econrel_fd + govservices_fd_lag + pervote_fd_lag + 
                       pervote_govservices_lag + factor(countryname),
                     data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                    party==13951, list(parfam=70),
                                                    party==14820, list(niche=1),
                                                    party==14820, list(parfam=70)) %>% 
                       filter(vote_av>=1 & parfam<80),
                     cluster="clusters")
  summary(reg9)
  length(reg9$lm_res[[2]]) 
  
  reg10 <- lm.cluster(libcon_fd ~ 0 + position.soc_fd + libcon_fd_lag + pervote_fd_lag + 
                        pervote_libcon_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg10)
  length(reg10$lm_res[[2]]) 
  
  reg11 <- lm.cluster(libcon_auth_fd ~ 0 + position.soc_fd + libcon_auth_fd_lag + pervote_fd_lag + 
                        pervote_libcon_auth_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg11)
  length(reg11$lm_res[[2]]) 
  
  
  

  
  
  #################         
  
  # domain-specific representation with the new scales: niche/mainstream
  
  reg12 <- lm.cluster(freemarket_fd ~ 0 + position.econabs_fd + niche + niche_position.econabsfd + freemarket_fd_lag + pervote_fd_lag + 
                        pervote_freemarket_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg12)
  length(reg12$lm_res[[2]]) 
  
  reg13 <- lm.cluster(govservices_fd ~ 0 + position.econrel_fd + niche + niche_position.econrelfd + govservices_fd_lag + pervote_fd_lag + 
                        pervote_govservices_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg13)
  length(reg13$lm_res[[2]]) 
  
  
  
  reg14 <- lm.cluster(libcon_fd ~ 0 + position.soc_fd + niche + niche_position.socfd + libcon_fd_lag + pervote_fd_lag + 
                        pervote_libcon_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg14)
  length(reg14$lm_res[[2]]) 
  
  reg15 <- lm.cluster(libcon_auth_fd ~ 0 + position.soc_fd + niche + niche_position.socfd + libcon_auth_fd_lag + pervote_fd_lag + 
                        pervote_libcon_auth_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg15)
  length(reg15$lm_res[[2]]) 
  
  
  #################         
  
  # domain-specific representation with the new scales: harmful/benign
  
  reg16 <- lm.cluster(freemarket_fd ~ 0 + harmful_econabs + benign_econabs + freemarket_fd_lag + pervote_fd_lag + 
                        pervote_freemarket_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg16)
  length(reg16$lm_res[[2]]) 
  
  reg17 <- lm.cluster(govservices_fd ~ 0 + harmful_econrel + benign_econrel + govservices_fd_lag + pervote_fd_lag + 
                        pervote_govservices_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg17)
  length(reg17$lm_res[[2]]) 
  
  reg18 <- lm.cluster(libcon_fd ~ 0 + harmful_soc + benign_soc + libcon_fd_lag + pervote_fd_lag + 
                        pervote_libcon_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg18)
  length(reg18$lm_res[[2]]) 
  
  reg19 <- lm.cluster(libcon_auth_fd ~ 0 + harmful_soc + benign_soc + libcon_auth_fd_lag + pervote_fd_lag + 
                        pervote_libcon_auth_lag + factor(countryname),
                      data=manifdata %>% mutate_when(party==13951, list(niche=1),
                                                     party==13951, list(parfam=70),
                                                     party==14820, list(niche=1),
                                                     party==14820, list(parfam=70)) %>% 
                        filter(vote_av>=1 & parfam<80),
                      cluster="clusters")
  summary(reg19)
  length(reg19$lm_res[[2]]) 
  
